package com.sgcc.ln.pmos.e_ln_home.activity;

import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.ln.pmos.arouter.home.HomeArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleActivity;
import com.sgcc.ln.pmos.e_ln_home.adapter.InfomationListAdapter;
import com.sgcc.ln.pmos.e_ln_home.adapter.InfomationTypeListAdapter;
import com.sgcc.ln.pmos.e_ln_home.bean.ByConditionBean;
import com.sgcc.ln.pmos.e_ln_home.bean.ByPublishTypeBean;
import com.sgcc.ln.pmos.e_ln_home.bean.DisclosureApplyBean;
import com.sgcc.ln.pmos.e_ln_home.bean.InfomationListBean;
import com.sgcc.ln.pmos.e_ln_home.contract.InfomationListContract;
import com.sgcc.ln.pmos.e_ln_home.contract.InfomationTypeListContract;
import com.sgcc.ln.pmos.e_ln_home.presenter.InfomationListPresenter;
import com.sgcc.ln.pmos.e_ln_home.presenter.InfomationTypeListPresenter;

/**
 * 信息披露 --- 列表
 */
@Route(path = HomeArouterPath.HOME_INFOMATIONTYPELIST)
public class InfomationTypeListActivity extends MvpBaseRecycleActivity<DisclosureApplyBean.DataBean.ListBean, InfomationTypeListPresenter> implements InfomationTypeListContract.View {

    private InfomationTypeListAdapter mInfomationTypeListAdapter;
    private String mLabel, mInfoCode = "", informationId = "";

    @Override
    protected void receiveIntentExtras() {
        super.receiveIntentExtras();
        mLabel = getIntent().getStringExtra("label");
        mInfoCode = getIntent().getStringExtra("infoCode");
    }

    @Override
    protected void initViews() {
        super.initViews();
        mPresenter.getByCondition(mInfoCode);
    }

    @Override
    public String getTitleText() {
        return mLabel;
    }

    @Override
    protected InfomationTypeListPresenter createPresenter() {
        return new InfomationTypeListPresenter();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mInfomationTypeListAdapter = new InfomationTypeListAdapter(mContext);
        mInfomationTypeListAdapter.setOnItemClickListener(new InfomationTypeListAdapter.OnItemClickListener() {
            @Override
            public void itemClickListener(int position, DisclosureApplyBean.DataBean.ListBean bean) {
                //发电企业--企业基本信息---fdqyQyjbxx  电力用户--企业基本信息---dlyhQyjbxx 售电公司--企业基本信息---sdgsQyjbxx
                if ("dlyhQyjbxx".equals(mInfoCode)) {
                    //发电企业--企业基本信息---fdqyQyjbxx  电力用户--企业基本信息---dlyhQyjbxx 售电公司--企业基本信息---sdgsQyjbxx
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_BASICINFORMATIONENTERPRISE)
                            .withString("guid", bean.getGuid())
                            .withString("sourceName", bean.getSourceName())
                            .navigation();
                } else if ("fdqyQyjbxx".equals(mInfoCode)) {
                    //发电企业--企业基本信息---fdqyQyjbxx  电力用户--企业基本信息---dlyhQyjbxx 售电公司--企业基本信息---sdgsQyjbxx
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_FDBASICINFORMATIONENTERPRISE)
                            .withString("guid", bean.getGuid())
                            .withString("sourceName", bean.getSourceName())
                            .navigation();
                } else if ("sdgsQyjbxx".equals(mInfoCode)) {
                    //发电企业--企业基本信息---fdqyQyjbxx  电力用户--企业基本信息---dlyhQyjbxx 售电公司--企业基本信息---sdgsQyjbxx
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_SDBASICINFORMATIONENTERPRISE)
                            .withString("guid", bean.getGuid())
                            .withString("sourceName", bean.getSourceName())
                            .navigation();
                } else if ("fdqyDcjzxx".equals(mInfoCode)) {
                    //电厂机组信息
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_DCJZXX)
                            .withString("guid", bean.getGuid())
                            .withString("sourceName", bean.getSourceName())
                            .withString("operateDate", bean.getOperateDate())
                            .withString("readAmount", bean.getReadAmount() + "")
                            .withString("publishType", bean.getPublishType())
                            .navigation();
                } else if ("sdgsZcrycl".equals(mInfoCode)) {
                    //售电公司---------资产人员材料
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_SDRYCL)
                            .withString("guid", bean.getGuid())
                            .withString("sourceName", bean.getSourceName())
                            .withString("operateDate", bean.getOperateDate())
                            .withString("readAmount", bean.getReadAmount() + "")
                            .withString("publishType", bean.getPublishType())
                            .navigation();
                } else if ("dlyhQyydxx".equals(mInfoCode)) {
                    //电力用户-----企业用电信息
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_DLYHQYYDXX)
                            .withString("guid", bean.getGuid())
                            .withString("sourceName", bean.getSourceName())
                            .withString("operateDate", bean.getOperateDate())
                            .withString("readAmount", bean.getReadAmount() + "")
                            .withString("publishType", bean.getPublishType())
                            .navigation();
                } else {
                    ARouter.getInstance()
                            .build(HomeArouterPath.HOME_INFOMATIONCONTENT)
                            .withString("title", bean.getTitle())
                            .withString("operateDate", bean.getOperateDate())
                            .withString("publishType", bean.getPublishType())
                            .withString("content", bean.getContent())
                            .withString("attachment", bean.getAttachment())
                            .withString("readAmount", bean.getReadAmount() + "")
                            .navigation();
                }
            }
        });
        return mInfomationTypeListAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        if (!TextUtils.isEmpty(informationId)) {
            mPresenter.queryIpDisclosureApply(mInfoCode, informationId, listSize, isloading);
        }
    }

    @Override
    public void getQueryIpDisclosureApplySuccess(DisclosureApplyBean response) {
        if (response.getData() != null) {
            loadinglayout.showContent();
            if (response.getData().getList() != null) {
                int total = response.getData().getTotal();
                int pageNum = response.getData().getPageNum();
                int pageSize = response.getData().getPageSize();
                boolean nextPage = false;
                if (total != 0) {
                    // 优先计算总页数
                    int totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
                    //下一页等于当前页+1,但不能超过总页数界限
                    nextPage = pageNum + 1 <= totalPage ? true : false;
                }
                loadSuccess(nextPage, response.getData().getList());
            } else {
                loadinglayout.showEmpty();
            }
        } else {
            loadinglayout.showError();
        }
    }

    @Override
    public void getQueryIpDisclosureApplyFailed() {

    }

    @Override
    public void getByConditionSuccess(ByConditionBean response) {
        StringBuffer stringBuffer = new StringBuffer();
        if (response.getData() != null) {
            if (response.getData().size() > 0) {
                for (ByConditionBean.DataBean bean : response.getData()) {
                    stringBuffer.append(bean.getGuid() + ",");
                }

                informationId = stringBuffer.substring(0, stringBuffer.length() - 1);
                refresh(false);
            }
        }
    }

    @Override
    public void getByConditionFailed() {

    }
}
